﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Entity;
using Business; 

namespace WebApp
{
    public partial class Ordersforuser : System.Web.UI.Page
    {
        string strConnect;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        DataSet ds;
        protected void Page_Load(object sender, EventArgs e)
        {
            strConnect = "Data Source = (local); Database =ProjectSem3; Trusted_Connection = true";
            con = new SqlConnection(strConnect);
            ds = new DataSet();
            if (!Page.IsPostBack)
            {
                try
                {
                   Bindcus();
                   provine();
                   Service();
                   load();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
        }

        private DataTable load()
        {
            Orders_Business p = new Orders_Business();
            DataTable dt = new DataTable();
            try
            {
                dt = p.Load();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
            finally
            {
                p = null;
            }

            return dt;
        }
        protected void Bindcus()
        {
            try
            {
               
                if (con.State == ConnectionState.Closed)
                    con.Open();
                string q = "select cusID from customer where username = '" + Session["userid"].ToString() + "'";
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "customer");

                lblcusid.Text = ds.Tables[0].Rows[0][0].ToString();
                lblcusid.DataBind();


            }
            catch (SqlException ex)
            {
                //throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
        protected void provine()
        {
            try
            {
                cmd = new SqlCommand("SELECT * FROM province", con);
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();


                dr = cmd.ExecuteReader();
                DropDownList1.DataSource = dr;
                DropDownList1.DataTextField = "provinceName";
                DropDownList1.DataBind();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        protected void Service()
        {
            try
            {
                cmd = new SqlCommand("SELECT * FROM serviceprice", con);
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();


                dr = cmd.ExecuteReader();
                DropDownList2.DataSource = dr;
                DropDownList2.DataTextField = "sername";
                DropDownList2.DataBind();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Today;
            

            Orders_Entity orders = new Orders_Entity();
            Orders_Business insert = new Orders_Business();
            orders.cusID =Convert.ToInt32( lblcusid.Text.Trim());
            orders.receiveName = txtname.Text.Trim();
            orders.receiveAddress = txtaddress.Text.Trim();
            orders.receivePhone = txtphone.Text.Trim();
            orders.Weight =Convert.ToInt32( txtweight.Text.Trim());
            orders.Senddate = dt;
            orders.Receivedate = checkdatereceive();
            orders.provinceID = checkprovince();
            orders.serID = checkservice();
            orders.Price = Convert.ToInt32( price());
            orders.Status = txtstatus.Text.Trim();
            insert.Add_Orders(orders);
            load();
        }
        public DateTime checkdatereceive()
        {
            DateTime redt = DateTime.Today;
            
            if (DropDownList2.Text == "Fast")
            {
                redt = DateTime.Today.AddDays(3);
                return redt;
            }
            else
            {
                redt = DateTime.Today.AddDays(5);
                return redt;
            }
        }
        public int checkprovince()
        {
            try
            {
                string query = "select provinceID from province where provinceName='" + DropDownList1.Text + "'";
                cmd = new SqlCommand(query, con);
                cmd.CommandType = CommandType.Text;
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "province");
                return Convert.ToInt32(ds.Tables["province"].Rows[0][0].ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
                
            }
            finally
            {
                con.Close();
            }
        }
        public int checkservice()
        {
            try
            {
                string q = "select serID from serviceprice where sername='" + DropDownList2.Text + "'";
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "serviceprice");
                return Convert.ToInt32(ds.Tables["serviceprice"].Rows[0][0].ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
       
        public int priceprovince()
    {
        try
        {
            string q = "select pricehcmc from  province where provinceName='" + DropDownList1.Text + "'";
            cmd = new SqlCommand(q, con);
            cmd.CommandType = CommandType.Text;
            if (con.State == ConnectionState.Open)
                con.Close();
            con.Open();
            da = new SqlDataAdapter(cmd);
            da.Fill(ds, "province");
            return Convert.ToInt32(ds.Tables["province"].Rows[1][1].ToString());
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

        public int priceservice()
        {
            try
            {
                string q = "select serprice from  serviceprice where sername='" + DropDownList2.Text + "'";
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                if (con.State == ConnectionState.Open)
                    con.Close();
                con.Open();
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "serviceprice");
                return Convert.ToInt32(ds.Tables["serviceprice"].Rows[1][1].ToString());
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
        public double price()
        {
            double price;
            price = Convert.ToInt32(txtweight.Text)* (priceprovince() + priceservice()) ;
            return price;
        }
    }
}